# -*- coding: UTF-8 -*-

from numpy import *


def load_data_set():
	posting_list = [
		['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'],
		['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'],
		['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'],
		['stop', 'posting', 'stupid', 'worthless', 'garbage'],
		['mr', 'licks', 'ate', 'my', 'steak', 'how', 'to', 'top', 'him'],
		['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']
	]
	class_vec = [0, 1, 0, 1, 0, 1]
	return posting_list, class_vec


def create_vocab_list(date_set):
	vocab_set = set([])
	for doc in date_set:
		vocab_set = vocab_set | set(doc)

	return list(vocab_set)


def set_of_words_2_vec(vocab_list, input_set):
	return_vec = [0] * len(vocab_list)
	for word in input_set:
		if word in vocab_list:
			return_vec[vocab_list.index(word)] = 1
		else:
			print("the word %s is not in my vocabulary" % word)
	return return_vec


def train_nbo(train_matrix, train_category):
	num_docs = len(train_matrix)
	num_words = len(train_matrix[0])
	p_abusive = sum(train_category) / float(num_docs)
	p0_num = zeros(num_words)
	p1_num = zeros(num_words)
	p0_denom = 0.0
	p1_denom = 0.0
	for i in range(num_docs):
		if train_category[i] == 1:
			p1_num += train_matrix[i]
			p1_denom += sum(train_matrix[i])
		else:
			p0_num += train_matrix[i]
			p0_denom += sum(train_matrix[i])

	p1_vect = p1_num / p1_denom
	p0_vect = p0_num / p0_denom
	return p0_vect, p1_vect, p_abusive